package com.okami.filter;

import javax.servlet.Filter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: TanJingyu
 * @create: 2021-03-11 17:32
 **/
public class FilterChain implements javax.servlet.FilterChain {
    private Integer count = 0;
    private LinkedList<Filter> filters = new LinkedList<>();

    public List<Filter> getFilters() {
        return filters;
    }

    public void setFilters(LinkedList<Filter> filters) {
        this.filters = filters;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
        // 保证执行完所有过滤器
        if (count < filters.size()) {
            filters.get(count++ - 1).doFilter(request, response, this);
        }else {
            //所有过滤器都执行完了，执行该执行的业务逻辑
            // 。。。。。。。。业务逻辑
        }
    }
}
